// 频道数据
import { getUserChannels, getChannels } from '@/api/channels'
export default {
  namespaced: true,
  state: {
    channelList: [],
    allChannelList: []
  },
  mutations: {
    setChannelList(state, payload) {
      state.channelList = payload
    },
    addChannelList(state, payload) {
      state.channelList.push(payload)
    },
    delChannelList(state, payload) {
      state.channelList = state.channelList.filter(
        (item) => item.id !== payload.id
      )
    },
    setAllChannelList(state, payload) {
      state.allChannelList = payload
    }
  },
  actions: {
    async actions_getChannelList(context) {
      const res = await getUserChannels()
      console.log(res)
      // this.channelList = res.data.channels
      context.commit('setChannelList', res.data.channels)
      console.log(this.channelList)
    },
    async actions_getAllChannelList(context) {
      const res = await getChannels()
      console.log(res)
      // this.channelList = res.data.channels
      context.commit('setAllChannelList', res.data.channels)
    }
  },
  getters: {
    // 所有频道-我的频道数据
    optionsChannelList(state) {
      // 遍历所有数组，过滤我的频道中没有的数据
      // return state.allChannelList.filter((item) => {
      //   const res = state.channelList.some((channel) => channel.id === item.id)
      //   console.log(res)
      //   // true => 我的频道有了
      //   // false => 我的频道没有
      //   if (res) {
      //     return false
      //   } else {
      //     return true
      //   }
      // })
      // 简化
      return state.allChannelList.filter(
        (item) => !state.channelList.some((channel) => channel.id === item.id)
      )
    }
  }
}
